package com.example.yuntiku.Servlet;

import com.example.yuntiku.Service.*;
import com.example.yuntiku.Vo.ExamVo;
import com.example.yuntiku.dao.ExamDao;
import com.example.yuntiku.dao.PagerDao;
import com.example.yuntiku.dao.PagerquestionDao;
import com.example.yuntiku.dao.QuestionDao;
import com.example.yuntiku.dao.impl.*;
import com.example.yuntiku.entity.*;
import com.example.yuntiku.entity.Record;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

//使用三元
@WebServlet(name = "TonyiCeshiServlet", value = "/TonyiCeshiServlet")
public class TonyiCeshiServlet extends HttpServlet {
    TestrecordService testrecordService = new TestrecordService();
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        User user = (User) request.getSession().getAttribute("user");
        //获得考试id和试卷id
        String examId = request.getParameter("examId");
        String pagerId = request.getParameter("pagerId");

        //根据试卷id查询考试的题目编号
        PagerquestionDao pagerquestionDao = new PagerquestionDaoImpl();
        List<Integer> integers = pagerquestionDao.selectByPagerId(Integer.parseInt(pagerId));
        //根据题目查询所有的题目
        QuestionDao questionDao = new QuestionDaoImpl();
        List<Question> questions = new ArrayList<>();
        for (Integer id : integers) {
            questions.add(questionDao.selectTyp(id));
        }
        PagerDao pagerDao = new PagerDaoImpl();
        Pager pager = pagerDao.selectByExam(Integer.parseInt(examId));
        request.setAttribute("curriculum", questions);
        request.setAttribute("shujmc", pager.getPname());//试卷名称
        request.setAttribute("shijId", pager.getRid());//试卷Id

        //修改testrecord表的开始答题时间
        Testrecord testrecord = testrecordService.selectByUserIdAndExamId(user.getUid(), Integer.parseInt(examId));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        testrecord.setStarttime(format.format(new Date()));
        testrecordService.update(testrecord);

        //转发到刷题页面
        request.getRequestDispatcher("tongyiCeshi.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        User user = (User) request.getSession().getAttribute("user");
        //获得考试id和试卷id
        String examId = request.getParameter("examId");
        String pagerId = request.getParameter("pagerId");
        Pager pager = new PagerService().selectById(Integer.parseInt(pagerId));
        ExamService examService = new ExamService();

        //处理乱码让中文可以输出
        request.setCharacterEncoding("UTF-8");

        //获取用户开考的时间
        String kkshij = request.getParameter("kkshij");

        //获取这套卷子的题数
//        String tote =

        //获取是自测还是考试
        String ptype = request.getParameter("ptype");

        //获取试卷的ID
        String shijId = request.getParameter("shijId");

        //获取这些题目是否有父级
       // String shifyfj = request.getParameter("shifyfj");

        //获取试卷名称
        String pagerName = pager.getPname();

        //为考试表添加记录
//        Exam exam = new Exam();
//        exam.setPagerid(Integer.parseInt(shijId));
//        exam.setStarttime(kkshij);
//        //用户Id
//        exam.setTeacherid(2);
//        exam.setTesttime(kkshij);
//        exam.setEtype(Integer.parseInt(ptype));
//        int ii = examService.insertExam(exam);

        //为个人考试记录表添加信息
        //登录来了从作用域掉用户ID
        int cjr = 2;

        //获取题目编号
        String[] testrescordids = request.getParameterValues("testrescordid");

        //获取用户选中的答案
        String[] chooseanswers = request.getParameterValues("chooseanswer");

        //获取题目的章节知识点编号
       // String[] section = request.getParameterValues("knowledgeid");

        //获取题目的一级知识点编号
        //String[] knowledgeone = request.getParameterValues("knowledgeid");

        //获取题目的二级知识点编号
       // String[] knowledgeid = request.getParameterValues("knowledgeid");

        //获取这些题目是否有父级
       // String shifyfjyy = request.getParameter("shifyfj");

//        int zhishidbh = 10086;
//        if (shifyfjyy != "") {
//            zhishidbh = Integer.parseInt(shifyfjyy);
//        }
        Testrecord testrecord = testrecordService.selectByUserIdAndExamId(user.getUid(), Integer.parseInt(examId));

        QuestionService questionService = new QuestionService();
        //获取用户做了多少题目
        int ztshu = pager.getTote();
        int correct =0;//正确的题数
        List<Examanwer> examanwers = new ArrayList<>();
        for (int i = 0;i < testrescordids.length; i++) {
            String testrescordid = testrescordids[i]; //第几题的编号
            String chooseanswer = chooseanswers[i]; //第几题的答案
            if (chooseanswers[i] == "" || chooseanswers[i] == null) {
                ztshu = ztshu - 1;//题目总数-没有答案的题数=做了的题数
            }
            //根据题目的ID查询题目，判断用户的答案是否正确
            Question question = questionService.selectAll(Integer.parseInt(testrescordid));
            if(question.getAnswer().equals(chooseanswer)){
                correct++;//累加正确题数
            }

            Examanwer examanwer = new Examanwer();
            examanwer.setTestrescordid(testrecord.getId());
            examanwer.setChooseanswer(chooseanswer);
            examanwer.setRightanswers(question.getAnswer());
            examanwer.setPagerqustionid(Integer.parseInt(testrescordid)); //题目编号
            examanwers.add(examanwer);
        }

        //获取用户是主动提交的还是自动提交的
        String zdonhzd = request.getParameter("zdonhzd");
        testrecord.setAnswersum(ztshu);//答题总数
        testrecord.setScore(correct*2); //分数
        testrecord.setTerrace(2); //默认电脑提交
        testrecord.setPattern(Integer.parseInt(zdonhzd)); //默认手动提交
        testrecord.setCorrect(correct); //正确题数
        testrecord.setSubmit(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int iii = testrecordService.update(testrecord);

        ExamanwerService examanwerService = new ExamanwerService();
        examanwerService.insertExamanwer(examanwers);

        //为考试答题表添加信息并将答对的题目数量保存到个人考试记录表中
//        for (
//                int i = 0;
//                i < testrescordids.length; i++) {
//            String testrescordid = testrescordids[i]; //第几题的编号
//            String chooseanswer = chooseanswers[i]; //第几题的答案
//            ExamanwerService examanwerService = new ExamanwerService();
//            Examanwer examanwer = new Examanwer();
//            examanwer.setTestrescordid(iii);
//            examanwer.setPagerqustionid(Integer.parseInt(testrescordids[i]));
//            examanwer.setChooseanswer(chooseanswers[i]);
//            QuestionService questionService = new QuestionService();
//            Question question = questionService.selectAnswer(Integer.parseInt(testrescordids[i]));
//            examanwer.setRightanswers(question.getAnswer());
//            examanwerService.insertExamanwerDao(examanwer);
//        }
//
//        //查询用户答对了几题
//        ExamanwerService examanwerService = new ExamanwerService();
//        int correct = examanwerService.selectCorrect(iii);
//
//        //答对题数查出来后对考试记录表的对题数量进行修改
//        testrecordService.updateTestrecord(correct, iii);
//
//        //为个人刷题记录表添加信息
//        ExamanwerDaoImpl examanwerDao = new ExamanwerDaoImpl();
//        List<Examanwer> list = examanwerDao.selectBctim(iii);
//        String shuzu = "";
//        for (
//                Examanwer examanwer : list) {
//            if (!examanwer.getChooseanswer().isEmpty()) {
//                examanwer.setPagerqustionid(examanwer.getPagerqustionid());
//                shuzu = String.valueOf(examanwer.getPagerqustionid());
//                QuestionDaoImpl questionDao = new QuestionDaoImpl();
//                Question question = questionDao.selectTyp(Integer.parseInt(shuzu));
//                System.out.println(shuzu);
//                System.out.println(question.getKnowledgetwo());
//                RecordService recordService = new RecordService();
//                int i = recordService.selectRecord(question.getKnowledgetwo());
//                System.out.println(i);
//                if (zhishidbh != 0) {
//                    if (i > 0) {
//                        int iy = recordService.updateRecordlist(Integer.parseInt(shuzu), String.valueOf(question.getKnowledgetwo()));
//                        System.out.println(iy);
//                    } else {
//                        com.example.yuntiku.entity.Record record = new com.example.yuntiku.entity.Record();
//                        record.setSection(question.getSection());
//                        record.setKnowledgeone(question.getKnowledgeone());
//                        record.setKnowledgeid(question.getKnowledgetwo());
//                        record.setPercentum(0);
//                        record.setSid(2);
//                        record.setRecordlist(shuzu);
//                        recordService.insertRecord(record);
//                    }
//
//                    //计算单元知识点的答题进度
//                    int iop = questionDao.selectTmshul(question.getKnowledgetwo());
//                    int mytddfb = 100 / iop;
//                    RecordDaoImpl recordDao = new RecordDaoImpl();
//                    Record record = recordDao.selectTmu(question.getKnowledgetwo());
//                    String[] recordlistArray = record.getRecordlist().split(",");
//                    System.out.println("recordlistArray.length = " + recordlistArray.length);
//                    System.out.println("iop==recordlistArray.length  " + (iop == recordlistArray.length));
//                    if (iop == recordlistArray.length) {
//                        System.out.println("100----------");
//                        recordDao.updateCompleteness(100, question.getKnowledgetwo());
//                    } else {
//                        System.out.println("66----------");
//                        int jishu = mytddfb * recordlistArray.length;
//                        recordDao.updateCompleteness(jishu, question.getKnowledgetwo());
//                    }
//                }
//            }
//        }
//        request.setAttribute("list", testrecord.getId());
//        request.setAttribute("topic", pagerName);
        response.sendRedirect("ExamanwerServlet?list=" + testrecord.getId()+"&topic="+pagerName);
    }
}